package com.sky.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.constant.MessageConstant;
import com.sky.constant.StatusConstant;
import com.sky.dto.CategoryDTO;
import com.sky.dto.CategoryPageQueryDTO;
import com.sky.entity.Category;
import com.sky.entity.Dish;
import com.sky.entity.Setmeal;
import com.sky.exception.StartOrStopFiledException;
import com.sky.mapper.CategoryMapper;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.result.PageResult;
import com.sky.service.CategoryService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Collections;
import java.util.Comparator;
import java.util.List;

@Service
public class CategoryServiceImpl implements CategoryService {

    @Autowired
    private CategoryMapper categoryMapper;
    @Autowired
    private DishMapper dishMapper;
    @Autowired
    private SetmealMapper setmealMapper;

    /**
     * 分类分页查询
     * @param categoryPageQueryDTO
     * @return
     */
    @Override
    public PageResult pageQuery(CategoryPageQueryDTO categoryPageQueryDTO) {
        //分页查询
        PageHelper.startPage(categoryPageQueryDTO.getPage(), categoryPageQueryDTO.getPageSize());
        Page<Category> page = categoryMapper.pageQuery(categoryPageQueryDTO);
        //对查询结果进行排序
        List<Category> categoryList = page.getResult();
        Collections.sort(categoryList, new Comparator<Category>() {
            @Override
            public int compare(Category o1, Category o2) {
                return o1.getSort() - o2.getSort();
            }
        });

        return new PageResult(page.getTotal(), categoryList);
    }

    /**
     * 根据分类类型查询分类
     * @param type
     * @return
     */
    @Override
    public List<Category> getByType(Integer type) {
        List<Category> categoryList = categoryMapper.getByType(type);
        return categoryList;
    }

    /**
     * 修改分类
     * @param categoryDTO
     */
    @Override
    public void update(CategoryDTO categoryDTO) {
        Category category = new Category();
        BeanUtils.copyProperties(categoryDTO, category);
        categoryMapper.update(category);
    }

    /**
     * 启用、禁用菜品分类
     * @param status
     * @param id
     */
    @Override
    @Transactional
    public void startOrStop(Integer status, Long id) {
        //通过菜品分类id去查询菜品，判断菜品是否在起售
        List<Dish> dishList =  dishMapper.getByCategoryIdAndStatus(id, StatusConstant.ENABLE);
        if (dishList.size() > 0) {
            throw new StartOrStopFiledException(MessageConstant.CATEGORY_NOT_STOP_BY_DISH);
        }

        //通过菜品分类id去查询套餐，判断套餐是否在起售
        List<Setmeal> setmealList = setmealMapper.getByCategoryId(id, StatusConstant.ENABLE);
        if (setmealList.size() > 0) {
            throw new StartOrStopFiledException(MessageConstant.CATEGORY_BE_RELATED_BY_SETMEAL);
        }

        //上述都没有则可以停售
        Category category = new Category();
        category.setId(id);
        category.setStatus(status);
        categoryMapper.update(category);

    }

    /**
     * 新增分类
     * @param categoryDTO
     */
    @Override
    public void addCategory(CategoryDTO categoryDTO) {
        Category category = new Category();
        BeanUtils.copyProperties(categoryDTO, category);
        //默认是启用状态
        category.setStatus(StatusConstant.ENABLE);
        categoryMapper.insert(category);
    }

    /**
     * 根据id删除分类
     * @param id
     */
    @Override
    public void deleteCategory(Long id) {
        categoryMapper.deleteById(id);
    }

    @Override
    public List<Category> list(Integer type) {
        List<Category> categoryList = categoryMapper.list(type);

        return categoryList;
    }
}
